<template>
  <el-dialog
    title="门店授权"
    @close="cancel"
    :visible.sync="dialogVisible"
    width="50%">
    <el-table v-loading="loading" lazy :data="userList" row-key="id"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="用户编号" align="center" key="userId" prop="userId"/>
      <el-table-column label="用户名称" align="center" key="userName" prop="userName" :show-overflow-tooltip="true"/>
      <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" :show-overflow-tooltip="true"/>
      <el-table-column label="组织架构" align="center" key="deptName" prop="dept.deptName" :show-overflow-tooltip="true"/>
      <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" width="120"/>
      <el-table-column label="状态" align="center" key="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :disabled="multiple">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
//import loadBMap from '@/utils/loadBMap.js'

import {changeUserStatus, listNotBranchAdmin} from "@/api/system/user";
import {branchAdminAdd} from "@/api/merchant/stores";

export default {
  name: "addStoreAdmin",
  data() {
    return {
      userList: [],
      total:0,
      loading: false,
      dialogVisible: false,
      ids: [],
      single: false,
      multiple: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bid:''
      },
    }
  }, methods: {
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.userName + '"用户吗？').then(function () {
        return changeUserStatus(row.userId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    submitForm() {
      this.$modal.confirm('是否确认授权用户编号为"' + this.ids + '"的位门店店长？').then( ()=> {
        for (let i = 0; i < this.ids.length; i++) {
          let uid=this.ids[i]
          branchAdminAdd({
            bid:this.queryParams.bid,
            uid:uid
          }).then(res=>{
            if (res.code == 200) {
              this.$modal.msgSuccess("添加成功");
              this.dialogVisible=false
              this.$emit('update')
            }
          })
        }
      }).catch(() => {
      });
    },
    cancel() {
      this.dialogVisible = false
    },
    getList(){
      listNotBranchAdmin(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    open(bid) {
      this.queryParams.bid=bid
      this.dialogVisible = true
      this.getList()
    }
  }
}
</script>
<style scoped lang="scss">

</style>
